package com.bosssoft.springcloud.controller;

import com.bosssoft.springcloud.entities.CommenResult;
import com.bosssoft.springcloud.entities.Payment;
import com.bosssoft.springcloud.service.PaymentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Value("${server.port}")
    private String serverport;
    @Resource
    private org.springframework.cloud.client.discovery.DiscoveryClient discoveryClient;

    /***
     * 查询订单列表
     * @return 订单列表
     */
    @RequestMapping("/payment/listAll")
    public CommenResult getOrderList(){
        List<Payment> orderList = paymentService.getOrderList();
        if (orderList!=null){
            return  new CommenResult(200,"订单列表成功生成 serverport: "+serverport,orderList);
        }
        return new CommenResult(444,"订单列表获取失败",null);
    }


    /***
     * 添加订单
     * @param payment 订单
     * @return
     */
    @RequestMapping("/payment/create")
    public CommenResult create(@RequestBody  Payment payment){

        int result=paymentService.create(payment);
        if(result>0){
            return  new CommenResult(200,"添加订单成功 serverport: "+serverport,result);
        }else {
             return  new CommenResult(444,"添加订单失败",null);
        }
    }

    /***
     * 根据订单号查询订单
     * @param id 订单号
     * @return 订单
     */
    @GetMapping  ("/payment/get/{id}")
    public CommenResult getPayentById(@PathVariable("id") long id){
        Payment payment = paymentService.getPayentById((int) id);

        if(payment!=null){

            return  new CommenResult(200,"查询订单成功 serverport: "+serverport,payment);
        }else {
            return  new CommenResult(444,"没有对应订单",null);
        }
    }

    /***
     * 根据id删除订单
     * @param id
     */
    @RequestMapping  ("/payment/remove/{id}")
    public CommenResult removeById(@PathVariable("id") long id){
        int num = paymentService.removeById(id);
        if (num>0){
            return  new CommenResult(200,"成功移除该订单 serverport: "+serverport,paymentService.getOrderList());
        }
        return new CommenResult(444,"没有对应订单",null);

    }

    /***
     * 返回该服务在注册中心的名称
     * @return
     */
    @GetMapping("/payment/discovery")
    public Object discovery(){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {

        }
        return this.discoveryClient;
    }

    /***
     * 返回该服务端口号
     * @return
     */
    @GetMapping("/payment/sp")
    public String getsp(){
        return  serverport;
    }

}
